<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="订单号" prop="orderId">
          <el-input v-model="dataForm.orderId" placeholder="订单号"></el-input>
      </el-form-item>
          <el-form-item label="退款订单号" prop="refundId">
          <el-input v-model="dataForm.refundId" placeholder="退款订单号"></el-input>
      </el-form-item>
          <el-form-item label="退款单号" prop="flowId">
          <el-input v-model="dataForm.flowId" placeholder="退款单号"></el-input>
      </el-form-item>
          <el-form-item label="票号" prop="ticketNumber">
          <el-input v-model="dataForm.ticketNumber" placeholder="票号"></el-input>
      </el-form-item>
          <el-form-item label="客户-姓名" prop="customerName">
          <el-input v-model="dataForm.customerName" placeholder="客户-姓名"></el-input>
      </el-form-item>
          <el-form-item label="证件号" prop="idCard">
          <el-input v-model="dataForm.idCard" placeholder="证件号"></el-input>
      </el-form-item>
          <el-form-item label="电话" prop="mobile">
          <el-input v-model="dataForm.mobile" placeholder="电话"></el-input>
      </el-form-item>
          <el-form-item label="航线" prop="routeName">
          <el-input v-model="dataForm.routeName" placeholder="航线"></el-input>
      </el-form-item>
          <el-form-item label="出票舱位" prop="ticketingPosition">
          <el-input v-model="dataForm.ticketingPosition" placeholder="出票舱位"></el-input>
      </el-form-item>
          <el-form-item label="退款价格" prop="refundAmount">
          <el-input v-model="dataForm.refundAmount" placeholder="退款价格"></el-input>
      </el-form-item>
          <el-form-item label="退款损失" prop="refundLossAmount">
          <el-input v-model="dataForm.refundLossAmount" placeholder="退款损失"></el-input>
      </el-form-item>
          <el-form-item label="退改状态" prop="refundStatus">
          <el-input v-model="dataForm.refundStatus" placeholder="退改状态"></el-input>
      </el-form-item>
          <el-form-item label="退改类型" prop="refundType">
          <el-input v-model="dataForm.refundType" placeholder="退改类型"></el-input>
      </el-form-item>
              <el-form-item label="更新者" prop="updater">
          <el-input v-model="dataForm.updater" placeholder="更新者"></el-input>
      </el-form-item>
          <el-form-item label="更新时间" prop="updateDate">
          <el-input v-model="dataForm.updateDate" placeholder="更新时间"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        orderId: '',
        refundId: '',
        flowId: '',
        ticketNumber: '',
        customerName: '',
        idCard: '',
        mobile: '',
        routeName: '',
        ticketingPosition: '',
        refundAmount: '',
        refundLossAmount: '',
        refundStatus: '',
        refundType: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        orderId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        refundId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        flowId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ticketNumber: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        customerName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        idCard: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        routeName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ticketingPosition: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        refundAmount: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        refundLossAmount: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        refundStatus: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        refundType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updater: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/flight/refund/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/flight/refund/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
